package com.why.rpc.core.server.hook;

import lombok.extern.slf4j.Slf4j;
import com.why.rpc.core.common.utils.NacosUtil;
import com.why.rpc.core.server.pool.ThreadPoolFactroy;

import java.util.concurrent.ExecutorService;

/**
 * @program: why-rpc-framework
 * @description:
 * @author: @why
 * @create: 2021-03-17 11:49
 **/
@Slf4j
public class ServerLogOut {

    private static final ServerLogOut logOut = new ServerLogOut();

    private ExecutorService poolFactory = ThreadPoolFactroy.getExecutorService("shutdown-hook");

    private ServerLogOut(){

    }
    public static ServerLogOut logOut() {
        return logOut;
    }

    public void clearAllService() {
        log.info("关闭服务器后将会去注册中心注销服务。。。");
        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            log.info("清除所有注册的服务");
            NacosUtil.clearRegistry();
            log.info("服务清除完成。");
            poolFactory.shutdown();
        }));

    }
}
